package com.bst.bsj.base.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.portal.util.ControllerUtil;
import com.bst.bsj.spring.util.ConfigUtil;
import com.homewaimai.common.lang.StringUtil;
import com.homewaimai.framework.web.request.session.LoginUser;

public class WebBaseController extends JsonBaseController {

    protected static final Log logger = LogFactory.getLog(WebBaseController.class);
    
    protected static final Integer numPerPage = 20;//每页总数  PAGE_SIZE

    protected ModelAndView getBopsModelAndView(String viewName) {
        ModelAndView mv = new ModelAndView("template/bops/" + viewName);
        return mv;
    }

    protected ModelAndView getFrontModelAndView(String viewName) {
        ModelAndView mv = new ModelAndView("template/front/" + viewName);
        return mv;
    }

    protected Map<String, Object> getBopsParams(HttpServletRequest request) {
        Map<String, Object> ps = PageData(request);
        ps.put("imageDomain",  ConfigUtil.getProperties().getProperty("image.domain")); //图片服务器路径
        ps.put("subMenu",  ps.get("subMenu"));  //子菜单id
        return ps;
    }

    
     
    public Map<String,Object> PageData(HttpServletRequest request){
     
      Map<String,Object> properties = request.getParameterMap();
      Map<String,Object> returnMap = new HashMap<String,Object>(); 
      Iterator entries = properties.entrySet().iterator(); 
      Map.Entry entry; 
      String name = "";  
      String value = "";  
      while (entries.hasNext()) {
        entry = (Map.Entry) entries.next(); 
        name = (String) entry.getKey(); 
        Object valueObj = entry.getValue(); 
        if(null == valueObj){ 
          value = ""; 
        }else if(valueObj instanceof String[]){ 
          String[] values = (String[])valueObj;
          for(int i=0;i<values.length;i++){ 
             value = values[i] + ",";
          }
          value = value.substring(0, value.length()-1); 
        }else{
          value = valueObj.toString(); 
        }
        returnMap.put(name, value); 
      }
      return  returnMap;
    }
    
    /**
     * 通用参数对象
     * 
     * @return
     */
    protected Map<String, Object> getCommonParams() {
        return ControllerUtil.getCommonParams();
    }

    /**
     * 输出
     * 
     * @param response
     * @param result
     */
    protected void write(HttpServletResponse response, String result) {
        ControllerUtil.write(response, result);
    }

    protected String returnJsonResult(String code, Object data) {
        return ControllerUtil.returnJsonResult(code, data);
    }

    protected void writeJSON(HttpServletResponse response, String code, Object data) {
        String jsonResult = returnJsonResult(code, data);
        write(response, jsonResult);
    }

    protected LoginUser getLoginUser(HttpServletRequest request) {
        return ControllerUtil.getLoginUser(request);
    }

    protected Map<String, Object> getAllParams(HttpServletRequest request) {
        return ControllerUtil.getAllParams(request);
    }

    protected void fillParams(Object obj, ModelMap modelMap) {
        try {
            ControllerUtil.fillParams(obj, modelMap);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            logger.error("ControllerUtil.fillParams(obj, modelMap)", e);
        }
    }

    public String forward(String url) {

        if (StringUtil.isNotBlank(url)) {
            return url;
        }

        return "/";
    }

    public boolean isLogined(HttpServletRequest request) {

        LoginUser u = getLoginUser(request);
        if (u == null || StringUtil.isBlank(u.getMemberId())) {
            return false;
        }

        return true;
    }

    public boolean isIpad(String userAgent) {
        if (userAgent.indexOf("iPad") != -1 || userAgent.indexOf("ipad") != -1) {
            return true;
        }
        return false;
    }

    public boolean isMicroMessage(String userAgent) {
        if (userAgent.indexOf("micromessenger") != -1) {
            return true;
        }
        return false;
    }

    public boolean isIphone(String userAgent) {
        if (userAgent.indexOf("iphone") != -1) {
            return true;
        }
        return false;
    }

    public boolean isAndroid(String userAgent) {
        if (userAgent.indexOf("android") != -1) {
            return true;
        }
        return false;
    }

}
